<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-4-20
 * Time: 下午1:19
 * 映射模式 -- 如常用于ORM,将sql映射到对象操作
 * 下例对一个mapping表进行操作
 */

namespace Lib;


use Lib\Db\MySQLi;

class Mapping
{
    public $id;
    public $name;
    public $mobile;
    public $regtime;

    protected $db;

    function __construct($id)
    {
        $this->db = new MySQLi();
        $this->db->connect('127.0.0.1','root','st826115','es_pool');
        $res = $this->db->query("select * from mapping where id=$id limit 1");
        $data = $res->fetch_assoc();
        $this->id = $id;
        $this->mobile = $data['mobile'];
        $this->name = $data['name'];
        $this->regtime = $data['regtime'];
    }

    function __destruct()
    {
        // TODO: Implement __destruct() method.
        $this->db->query("update mapping set name = '{$this->name}', mobile='{$this->mobile}', regtime='{$this->regtime}' where id={$this->id}");
    }
}